package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityAsrFeaturesBean;
import cc.comac.mom.bean.QualityQarBasicStandardAsrFeatures;
import cc.comac.mom.dao.entity.QualityQarBasicStandardAsrFeaturesDao;
import cc.comac.mom.entity.QualityQarBasicStandardAsrFeaturesEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
@Component
@Transactional
public class QualityQarBasicStandardAsrFeaturesDataHandler extends ServiceImpl<QualityQarBasicStandardAsrFeaturesDao, QualityQarBasicStandardAsrFeaturesEntity>{

	@Autowired
	private QualityQarBasicStandardAsrFeaturesDao qualityQarBasicStandardAsrFeaturesDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	@Autowired
	private QualityAsrFeaturesDataHandler qualityAsrFeaturesDataHandler;
	/**
	 * 批量新增asr特征库数据
	 * **/
	public ResultBean batchInsertQarBasicStandardAsrFeatures(List<QualityQarBasicStandardAsrFeatures> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list.size()> 0) {
			List<QualityQarBasicStandardAsrFeaturesEntity> entityList = new ArrayList<QualityQarBasicStandardAsrFeaturesEntity>();
			for (QualityQarBasicStandardAsrFeatures qualityQarBasicStandardAsrFeatures : list) {
				QualityQarBasicStandardAsrFeaturesEntity innerData = qualityQarBasicStandardAsrFeatures.getInnerData();
				if (innerData.getAsrFeaturesId() == 0) {
					throw new RuntimeException("无法获取asr特征库主键");
				}
				if (innerData.getQarBasicStandardId() == 0) {
					throw new RuntimeException("无法获取qar母本主键");
				}
				baseEntityDataUtil.setBaseEntityData(innerData);
				entityList.add(innerData);
			}
			// 批量新增对应数据
			saveBatch(entityList, entityList.size());
		}
		return resultBean;
	}

	/**
	 * 通过qar母本主键获取对应数据
	 * **/
	public List<QualityAsrFeaturesBean> selectByQarBasicStandardId(long qarBasicStandardId) {
		List<QualityAsrFeaturesBean> selectByIds = null;
		// 通过qar母本主键获取对应的特征库数据对应的主键集合
		QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("qar_basic_standard_id", qarBasicStandardId);
		List<QualityQarBasicStandardAsrFeaturesEntity> selectList = qualityQarBasicStandardAsrFeaturesDao.selectList(queryWrapper);
		if (selectList.size() > 0) {
			// 当查询的特征库主键存在时查询对应特征库信息
			List<Long> beanList = new ArrayList<Long>();
			for (QualityQarBasicStandardAsrFeaturesEntity qualityQarBasicStandardAsrFeaturesEntity : selectList) {
				beanList.add(qualityQarBasicStandardAsrFeaturesEntity.getAsrFeaturesId());
			}
			selectByIds = qualityAsrFeaturesDataHandler.selectByIds(beanList);
			
		}
		return selectByIds;
	}

	/**
	 * 通过qar母本主键特征库对应数据，并提示哪些被选中了
	 * **/
	public List<QualityAsrFeaturesBean> selectFeaturesByQarBasicStandardId(long qarBasicStandardId, String partNumber) {
		
		// 通过qar母本主键获取对应的特征库数据对应的主键集合
		QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("qar_basic_standard_id", qarBasicStandardId);
		List<QualityQarBasicStandardAsrFeaturesEntity> selectList = qualityQarBasicStandardAsrFeaturesDao.selectList(queryWrapper);
		List<Long> beanList = new ArrayList<Long>();
		// 当特征库中存在对应数据时
		if (selectList.size() > 0) {
			// 当查询的特征库主键存在时查询对应特征库信息
			for (QualityQarBasicStandardAsrFeaturesEntity qualityQarBasicStandardAsrFeaturesEntity : selectList) {
				beanList.add(qualityQarBasicStandardAsrFeaturesEntity.getAsrFeaturesId());
			}
		} 
		// 通过零件材料图号获取对应特征库数据信息
		List<QualityAsrFeaturesBean> selectByPartNumberId = qualityAsrFeaturesDataHandler.selectByPartNumber(partNumber);
		List<QualityAsrFeaturesBean> resultList=new ArrayList<>();
		if (selectByPartNumberId.size() > 0) {
			// 对比特征库关联表中是否存在对应的特征库主键数据，存在标记一下
			for (QualityAsrFeaturesBean qualityAsrFeaturesBean : selectByPartNumberId) {
				for (Long FeaturesId : beanList) {
					if (qualityAsrFeaturesBean.getInnerData().getId() == FeaturesId) {
						/*qualityAsrFeaturesBean.setFlag(true);
						break;*/
						resultList.add(qualityAsrFeaturesBean);
					}
				}
			}
		}
		return resultList;
	}

	/**
	 * 通过qar母本主键逻辑删除对应数据
	 * **/
	public ResultBean batchDeleteByQarBasicStandardId(List<Long> qarBasicStandardIds) {
		ResultBean resultBean = new ResultBean(true);
		QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.in("qar_basic_standard_id", qarBasicStandardIds);
		List<QualityQarBasicStandardAsrFeaturesEntity> selectList = qualityQarBasicStandardAsrFeaturesDao.selectList(queryWrapper);
		if (selectList.size()> 0) {
			for (QualityQarBasicStandardAsrFeaturesEntity qualityQarBasicStandardAsrFeaturesEntity : selectList) {
				qualityQarBasicStandardAsrFeaturesEntity.setRowStatus(0);
				baseEntityDataUtil.updateBaseEntityData(qualityQarBasicStandardAsrFeaturesEntity);
			}
			updateBatchById(selectList, selectList.size());
		}
		return resultBean;
	}
	/**
	 * 通过qar母本主键删除对应数据
	 * **/
	public ResultBean removeDataByQarBasicStandardId(long qarBasicStandardId) {
		ResultBean resultBean = new ResultBean(true);
		QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity>();
		queryWrapper.eq("qar_basic_standard_id", qarBasicStandardId);
		qualityQarBasicStandardAsrFeaturesDao.delete(queryWrapper);
		return resultBean;
	}
	
	/**
	 * 通过qar母本主键删除对应数据集合
	 * **/
	public ResultBean removeDataByQarBasicStandardIds(List<Long> qarBasicStandardIds) {
		ResultBean resultBean = new ResultBean(true);
		QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity>();
		queryWrapper.in("qar_basic_standard_id", qarBasicStandardIds);
		qualityQarBasicStandardAsrFeaturesDao.delete(queryWrapper);
		return resultBean;
	}

	/**
	 * 新增或者新增特征库对应数据
	 * **/
	public ResultBean addOrUpdateDataList (List<QualityQarBasicStandardAsrFeatures> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list!=null&&list.size()>0) {
			List<Long> featureIds = new ArrayList<Long>();
			List<QualityQarBasicStandardAsrFeaturesEntity> entityList = new ArrayList<QualityQarBasicStandardAsrFeaturesEntity>();
			for (QualityQarBasicStandardAsrFeatures qualityQarBasicStandardAsrFeatures : list) {
				QualityQarBasicStandardAsrFeaturesEntity innerData = qualityQarBasicStandardAsrFeatures.getInnerData();
				featureIds.add(qualityQarBasicStandardAsrFeatures.getInnerData().getAsrFeaturesId());
				baseEntityDataUtil.setBaseEntityData(innerData);
				entityList.add(innerData);
			}
			// 删除对应数据
			QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity>();
			queryWrapper.in("asr_features_id", featureIds);
			queryWrapper.eq("qar_basic_standard_id", list.get(0).getInnerData().getQarBasicStandardId());
			qualityQarBasicStandardAsrFeaturesDao.delete(queryWrapper);
			// 新增对应数据
			saveBatch(entityList, entityList.size());
		}
		return resultBean;
	}
}
